﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using EDTS.Model.Users;
using EDTS.Model.Orgnizations;
namespace EDTS.Data.Users
{
    public class UserCommand
    {
        public static UserModel GetModel(Guid guid)
        {
            if (guid != Guid.Empty)
            {
                using (DBCentre db = new DBCentre())
                {
                    var d = db.DataContext.Users.FirstOrDefault(p => p.Guid == guid);
                    if (d != null)
                    {
                        var ret = Convertor.ConvertTo<UserModel, User>(d);
                        return ret;
                    }
                }
            }
            return null;
        }
        public static UserModel GetModel(string userName)
        {
            if (!string.IsNullOrEmpty(userName))
            {
                using (DBCentre db = new DBCentre())
                {
                    var d = db.DataContext.Users.FirstOrDefault(p => p.UserName == userName);
                    if (d != null)
                    {
                        var ret = Convertor.ConvertTo<UserModel, User>(d);
                        return ret;
                    }
                }
            }
            return null;
        }

        private static void SaveCheck(UserModel model, DBCentre db)
        {
            if (string.IsNullOrEmpty(model.UserName))
                throw new Utils.IException("请提供用户名");
            if (string.IsNullOrEmpty(model.Password))
                throw new Utils.IException("请提供密码");
            if (string.IsNullOrEmpty(model.RealName))
                throw new Utils.IException("请提供用户真实姓名");
            if (string.IsNullOrEmpty(model.Mobile))
                throw new Utils.IException("请提供手机号码");
            if (model.Unit==null || model.Unit.Guid==Guid.Empty)
                throw new Utils.IException("请提供所属单位");
            if (db != null)
            {
                if (db.DataContext.Users.Any(p=>p.Mobile==model.Mobile && p.Guid!=model.Guid))
                    throw new Utils.IException("此手机号码已被另一个用户使用，请检查。");
                var unit = db.DataContext.Units.FirstOrDefault(p => p.Guid == model.Unit.Guid);
                if (unit==null)
                    throw new Utils.IException("无效的所属单位");
                model.Unit = Convertor.ConvertTo<UnitModel, Unit>(unit);
                model.UnitID = unit.ID;
                if (db.DataContext.Users.Any(p=>p.UserName==model.UserName && p.Guid!=model.Guid))
                    throw new Utils.IException("用户名重复，请另选一个。");
            }
        }

        public static void CreateNew(UserModel model)
        {
            if (model != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    model.CreatedOn = DateTime.Now;
                    model.LastLoginTime = DateTime.Now;
                    model.Guid = Guid.NewGuid();
                    model.Status = (int)Model.Status.Active;

                    SaveCheck(model, db);
                    var d = Convertor.ConvertFrom<User, UserModel>(model);
                    db.DataContext.Users.Add(d);
                    db.DataContext.SaveChanges();
                    model.ID = d.ID;
                }
            }
        }

        public static void Update(UserModel model)
        {
            if (model != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    var d = db.DataContext.Users.FirstOrDefault(p => p.Guid == model.Guid);
                    if (d == null) throw new Utils.IException("无效用户");
                    SaveCheck(model, db);

                    Convertor.UpdateValue(model, d);
                    db.DataContext.SaveChanges();
                }
            }
        }

        public static void Interest(string userName,bool it)
        {
            if (!string.IsNullOrEmpty(userName))
            {
                using (DBCentre db = new DBCentre())
                {
                    var d = db.DataContext.Users.FirstOrDefault(p => p.UserName == userName);
                    if (d != null)
                    {
                        if (d.Interested != it)
                        {
                            d.Interested = it;
                            db.DataContext.SaveChanges();
                        }
                    }
                }
            }
        }
    }
}
